package SwordOffer2;

import java.util.ArrayList;

public class day8_3 {
    /*
     * 1.创建一个动态数组arrays存放链表节点，并计算链表长度
     * 2.创建left，right指针，当left<right，使left指向right，right指向left下个节点，
     * 3.若长度为奇数，循环结束，left指向null，若为偶数，当left.next=right时，left指向null，退出循环
     * */
    public void reorderList(ListNode head) {
        int len = 0;
        ArrayList<ListNode> arrays = new ArrayList<>();
        while (head != null) {
            arrays.add(head);
            head = head.next;
            len++;
        }
        // System.out.println(len);
        int left = 0;
        int right = len - 1;
        while (left < right) {
            ListNode temp = arrays.get(left).next;
            arrays.get(left).next = arrays.get(right);
            if (arrays.get(right) == temp) {
                arrays.get(right).next = null;
                break;
            }
            arrays.get(right).next = temp;
            left++;
            right--;
        }
        if (left == right) {
            arrays.get(left).next = null;
        }
    }
}
